package com.edu.policy.controller;

import com.edu.policy.common.api.CommonResult;
import com.edu.policy.domin.PageDto;
import com.edu.policy.model.UmsResource;
import com.edu.policy.security.component.DynamicSecurityMetadataSource;
import com.edu.policy.service.AppUmsResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台资源管理Controller
 * Created by chz on 2020/2/4.
 */
@Controller
@Api(tags = "UmsResourceController")
@RequestMapping("/resource")
@RequiredArgsConstructor
public class UmsResourceController {

    private final AppUmsResourceService resourceService;
    private final DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

    @ApiOperation("添加后台资源")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> create(@RequestBody UmsResource umsResource) {
        boolean count = resourceService.save(umsResource);
        dynamicSecurityMetadataSource.clearDataSource();
        if (count ) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台资源")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> update(@PathVariable Long id,
                                                 @RequestBody UmsResource umsResource) {
        umsResource.setId(id);
        boolean count = resourceService.updateById(umsResource);
        dynamicSecurityMetadataSource.clearDataSource();
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取资源详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsResource> getItem(@PathVariable Long id) {
        UmsResource umsResource = resourceService.getById(id);
        return CommonResult.success(umsResource);
    }

    @ApiOperation("根据ID删除后台资源")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> delete(@PathVariable Long id) {
        boolean count = resourceService.removeById(id);
        dynamicSecurityMetadataSource.clearDataSource();
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页模糊查询后台资源")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageDto<UmsResource>> list(@RequestParam(required = false) Long categoryId,
                                                   @RequestParam(required = false) String nameKeyword,
                                                   @RequestParam(required = false) String urlKeyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage) {
        PageDto<UmsResource> resourceList = resourceService.list(categoryId, nameKeyword, urlKeyword, pageSize, currentPage);
        return CommonResult.success(resourceList);
    }

    @ApiOperation("查询所有后台资源")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsResource>> listAll() {
        List<UmsResource> resourceList = resourceService.listAll();
        return CommonResult.success(resourceList);
    }
}
